package net.xiangcaowuyu.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 博客：https://www.xiangcaowuyu.net
 * Description:
 *
 * @Author: 香草物语
 * DateTime: 2021-07-25 16:14
 */
@RestController
@Slf4j
public class FlowLimitController {

    /***************************** 流量控制开始 **********************************/
    @GetMapping("/testQPSA")
    public String testQPSA() {
        return "-----------testQPSA";
    }

    @GetMapping("/testThreadA")
    public String testThreadA() {
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "-----------testThreadA";
    }

    @GetMapping("/testRelationA")
    public String testRelationA() {
        log.info(Thread.currentThread().getName() + "\t testRelationA");
        return "-----------testRelationA";
    }

    @GetMapping("/testRelationB")
    public String testRelationB() {
        log.info(Thread.currentThread().getName() + "\t testRelationB");
        return "-----------testRelationB";
    }

    @GetMapping("/testWarmup")
    public String testWarmup() {
        return "-----------testWarmup";
    }

    @GetMapping("/testQueue")
    public String testQueue() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "-----------testQueue";
    }

    /***************************** 流量控制结束 **********************************/

    /***************************** 熔断降级开始 **********************************/

    @GetMapping("/testRT")
    public String testRT() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "-----------testRT";
    }

    @GetMapping("/testExceptionPer")
    public String testExceptionPer() {
        int age = 1 / 0;
        return "-----------testExceptionPer";
    }

    /***************************** 熔断降级结束 **********************************/

    /***************************** 热点参数开始 **********************************/
    @GetMapping("/testHotkey")
//    @SentinelResource(value = "testHotkey", blockHandler = "deal_testHotkey")
    @SentinelResource(value = "testHotkey")
    public String testHotkey(@RequestParam(value = "productId", required = false) String productId,
                             @RequestParam(value = "userId", required = false) String userId) {
        return "--------------testHotkey";
    }

    /***************************** 热点参数结束 **********************************/

    @GetMapping("/testD")
    public String testD() {
        int age = 10 / 0;
        log.info(Thread.currentThread().getName() + "\ttestB");
        return "-----------testD";
    }

    public String deal_testHotkey(String p1, String p2, BlockException blockException) {
        return "--------------deal_testHotkey";
    }
}
